package com.skjanyou.applicationcase.util;

import java.awt.Image;
import java.awt.Toolkit;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;
/**
 * http://blog.csdn.net/shenzhennba/article/details/8213294
 * **/
public class ClipboardUtil {
	/**设置系统剪切板内容[内容为文本]**/
	public static void setSystemClipboard(String refContent){ 
		String vc = refContent.trim();
		StringSelection ss = new StringSelection(vc);
		Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null);  
	}
	/**设置系统剪切板内容[内容为图片型]**/
	public static void setImageClipboard(Image image) { 
		ImageSelection imgSel = new ImageSelection(image); 
		Toolkit.getDefaultToolkit().getSystemClipboard().setContents(imgSel, null); 
	} 
	/**获取系统剪切板内容[剪切板中内容为文本型]**/
	public static String getSystemClipboard(){//获取系统剪切板的文本内容[如果系统剪切板复制的内容是文本]
		Transferable t = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);  //跟上面三行代码一样
		try { 
			if (null != t && t.isDataFlavorSupported(DataFlavor.stringFlavor)) { 
				String text = (String)t.getTransferData(DataFlavor.stringFlavor); 
				return text; 
			} 
		} catch (UnsupportedFlavorException e) {
			//System.out.println("Error tip: "+e.getMessage());
		} catch (IOException e) { 
		} 	//System.out.println("Error tip: "+e.getMessage());
		return null; 
	}
	/**获取系统剪切板内容[剪切板中内容为图片型]**/
	public static Image getImageClipboard() { 
		Transferable t = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null); 
		try { 
			if (null  != t && t.isDataFlavorSupported(DataFlavor.imageFlavor)) { 
			Image image = (Image)t.getTransferData(DataFlavor.imageFlavor); 
			return image; 
			} 
		} catch (UnsupportedFlavorException e) { 
			  //System.out.println("Error tip: "+e.getMessage());
		} catch (IOException e) { 
			  //System.out.println("Error tip: "+e.getMessage());
		} 
		return null; 
	}
	
	public static class ImageSelection implements Transferable { 
		private Image image; 
		public ImageSelection(Image image) {this.image = image;} 

		public DataFlavor[] getTransferDataFlavors() { 
			return new DataFlavor[]{DataFlavor.imageFlavor}; 
		} 

		public boolean isDataFlavorSupported(DataFlavor flavor) { 
			return DataFlavor.imageFlavor.equals(flavor); 
		}

		public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException { 
			if (!DataFlavor.imageFlavor.equals(flavor)) {throw new UnsupportedFlavorException(flavor);} 
			return image; 
		}
	}
}
